package com.erdos.core.dao.ibatis;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.erdos.common.dao.IBatisGenericDaoImpl;
import com.erdos.common.exception.ConcurrentModifyException;
import com.erdos.common.exception.DaoException;
import com.erdos.common.web.LoginUtil;
import com.erdos.core.dao.ContentTextDao;
import com.erdos.core.entity.ContentText;
public class ContentTextDaoImpl extends IBatisGenericDaoImpl<ContentText> implements
		ContentTextDao {

	/**
	 * 按资源编码删除一组ContentText
	 * @param contentId
	 * @author jl
	 * @since  2008-7-22 
	 */
	public int deleteByContentId(String contentId) {
		return this.getSqlMapClientTemplate().delete("ContentText.deleteByContentId", contentId);
	}
	
	/**
	 * 更新ContentText
	 * @param ContentText
	 * @author jl
	 * @since  2008-7-22 
	 */
	public void update(ContentText contentText) {
		ContentText dbContentText =this.findByPrimaryKey(contentText.getSid());
		if (dbContentText.getVersion().compareTo(contentText.getVersion()) > 0) {
			throw new ConcurrentModifyException();
		}
		String updateSqlMapId = "ContentText.update";
		contentText.setUpdatedBy(LoginUtil.getUpcUserPin());
		contentText.setUpdatedDt(new Timestamp(new Date().getTime()));
		try {
			int effectCnt=this.getSqlMapClientTemplate().update(updateSqlMapId, contentText);
			if(effectCnt==0){
				throw new ConcurrentModifyException();
			}
		} catch (DataAccessException e) {
			throw new DaoException("jdbc.error.code.Common.update", new String[] { entityClass.getName(),contentText.getSid().toString()},e);
		}
	}
	

	/**
	 * 按资源编码查找一组ContentText
	 * @param contentId
	 * @return java.util.List
	 * @author jl
	 * @since  2008-7-22 
	 */
	@SuppressWarnings("unchecked")
	public List<ContentText> findByContentId(String contentId) {
		return this.getSqlMapClientTemplate().queryForList("ContentText.findByContentId", contentId);
	}
	
	/**
	 * 按主键查找ContentText
	 * @param contentId
	 * @return com.hp.core.entity.ContentText;
	 * @author jl
	 * @since  2008-7-22 
	 */
	public ContentText findByPrimaryKey(Long sid) {
		return (ContentText)this.getSqlMapClientTemplate().queryForObject("ContentText.findByPrimaryKey", sid);
	}
}
